#!/bin/bash
#
# @file     script/backup_project.sh
# @brief
#           local backup and remote backup projects
#           dump/restore cpio tar/pax dd
#
# @author   Spark Zheng
# @history
#           version 0.1.0
# 

POLICY=${1:-"0"}
work_dir="/home/zhengyu/Projects/mfs_projects/dserver"
back_dir="/home/zhengyu/Projects/backups"

svn_dir="/home/zhengyu/svn/my/test-servers/dserver"
remote_back_dir="/home/zhengyu/Projects/backups"
ssh_server="zhengyu@cas1"

function local_backup()
{
pushd "/tmp/"

	mkdir -p ${back_dir}

	tar_dir=$(dirname $1)
	tar_name=$(basename $1)
	timestamp=$(date +%Y%m%d)

	tar -C ${tar_dir} -cvjf ${tar_name}.${timestamp}.tar.bz2 ${tar_name}
	mv -f ${tar_name}.${timestamp}.tar.bz2 \
		${back_dir}/${tar_name}"_"${2}.${timestamp}.tar.bz2

popd

	return 0
}

function remote_backup()
{
pushd "/tmp/"

	ssh ${ssh_server} mkdir -p ${remote_back_dir}

	tar_dir=$(dirname $1)
	tar_name=$(basename $1)
	timestamp=$(date +%Y%m%d)

	tar -C ${tar_dir} -cvjf ${tar_name}.${timestamp}.tar.bz2 ${tar_name}
	scp ${tar_name}.${timestamp}.tar.bz2 \
		${ssh_server}:${back_dir}/${tar_name}"_"${2}.${timestamp}.tar.bz2
	rm ${tar_name}.${timestamp}.tar.bz2

popd

	return 0
}

#main flow
make -C "${work_dir}" clean

if [ "X$POLICY" == "X0" ]
then
	local_backup ${work_dir} work
	#local_backup ${svn_dir} svn
else
	remote_backup ${work_dir} work
	#remote_backup ${svn_dir} svn
fi

exit 0
#end main
